//
//  ScanViewController.m
//  FloatClouds
//
//  Created by 黄威 on 16/8/25.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "ScanViewController.h"
#import "TAMacro.h"
#import "PureLayout.h"
#import "HWLScan.h"
#import "UIView+Frame.h"
#import "HWLAudioTool.h"
#import "HWLNetworkTool.h"
#import "ProductDetailTableViewController.h"
@interface ScanViewController()
@property (nonatomic, weak) UIImageView *bg;
@property (nonatomic, weak) UIImageView *up;
@property (nonatomic, weak) UIImageView *down;
@end


@implementation ScanViewController

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self createNavigation];

}
-(void)viewDidLoad{
 
    
    UIImageView *bg = [[UIImageView alloc] init];
    bg.image = [UIImage imageNamed:@"FloatClouds"];
    bg.frame = self.view.bounds;
    [self.view addSubview:bg];
    self.bg = bg;
    
    
    UIImageView *up = [[UIImageView alloc] init];
    up.image = [UIImage imageNamed:@"bg_yaoyao_above"];
    up.frame = CGRectMake(0, 0, self.view.width, self.view.height * 0.5);
    [bg addSubview:up];
    self.up = up;
    
    
    UIImageView *down = [[UIImageView alloc] init];
    down.image = [UIImage imageNamed:@"bg_yaoyao_under"];
    down.frame = CGRectMake(0, self.view.height * 0.5, self.view.width, self.view.height * 0.5);
    [bg addSubview:down];
    self.down = down;

 
   }




- (void)createNavigation{
    
    //设置navigation相关属性
    
  
    NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil];
    
    [self.navigationController.navigationBar setTitleTextAttributes:attributes];
    self.navigationItem.title = @"摇一摇";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"扫一扫" style:UIBarButtonItemStylePlain target:self action:@selector(scan)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];

}


- (void)scan{

    HWLScan *Scan = [[HWLScan alloc]init];
    
    [self.navigationController pushViewController:Scan animated:YES];



}


-(BOOL)canBecomeFirstResponder
{
    return YES;
}


-(void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    
    [self becomeFirstResponder];
    
     self.bg.frame = self.view.bounds;
    self.up.frame = CGRectMake(0, 0, self.view.width, self.view.height * 0.5);
    self.down.frame = CGRectMake(0, self.view.height * 0.5, self.view.width, self.view.height * 0.5);
    
    self.tabBarController.tabBar.hidden = NO;
}


-(void)viewWillDisappear:(BOOL)animated {
    
    [self resignFirstResponder];
    
    [super viewWillDisappear:animated];
    
}

#pragma mark - 实现相应的响应者方法
/** 开始摇一摇 */
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
   
    
    [HWLAudioTool playMusic:@"5018.mp3"];
    
    CGFloat offset = self.bg.height ;
    CGFloat duration = 0.4;
    
    [UIView animateWithDuration:duration animations:^{
        self.up.y -= offset;
        self.down.y += offset;
    }];
    

    
}

/** 摇一摇结束（需要在这里处理结束后的代码） */
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
       // 不是摇一摇运动事件
    if (motion != UIEventSubtypeMotionShake) return;
    
 
    CGFloat offset = self.bg.height ;
    CGFloat duration = 0.4;
    [UIView animateWithDuration:duration animations:^{
        self.up.y += offset;
        self.down.y -= offset;
        
           }];
    
   
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        
        int value = (arc4random() % 3204) + 8;
        NSString *luckNumber = [NSString stringWithFormat:@"%d",value];
        [self handData:luckNumber];
        [HWLAudioTool playMusic:@"4092.wav"];
    });

    
}



/** 摇一摇取消（被中断，比如突然来电） */
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    
}


- (void)handData:(NSString *)qr{
    
   
    
        ProductDetailTableViewController *productDetail = [[ ProductDetailTableViewController alloc]init];
        productDetail.productId = qr;
        
        [self.navigationController pushViewController:productDetail animated:YES];
        
 
}



@end
